package com.sgb.contract.view.ui.contract;

import android.view.inputmethod.EditorInfo;

import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.base.BaseFragment;
import com.sgb.contract.databinding.ContractFragmentBinding;
import com.sgb.contract.model.ContractCategoryBean;
import com.sgb.contract.model.ContractKeyValueBean;
import com.sgb.contract.view.widget.pop.SelectPop;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：合同-分类碎片
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.11.04
 */
public class ContractItemFragment extends BaseFragment<ContractFragmentBinding, ContractItemModel> {

    private final BaseActivity baseActivity;
    private final ContractCategoryBean contractCategoryBean;
    private SelectPop mSelectPop1, mSelectPop2;

    /**
     * pop弹窗数据更新
     */
    private final List<ContractKeyValueBean> contractStateList = new ArrayList<>();
    private final List<ContractKeyValueBean> contractApprovalList = new ArrayList<>();

    public static ContractItemFragment newInstance(BaseActivity context, ContractCategoryBean categoryBean) {
        return new ContractItemFragment(context, categoryBean);
    }

    private ContractItemFragment(BaseActivity baseActivity, ContractCategoryBean categoryBean) {
        this.baseActivity = baseActivity;
        this.contractCategoryBean = categoryBean;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_fragment;
    }

    @Override
    protected void init() {
        initView();
        loadData();
    }

    private void initView() {
        mDataBinding.contractRlvView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp0_5), getResources().getColor(R.color.color_ffededed)));
        mSelectPop1 = new SelectPop(requireActivity());
        mSelectPop2 = new SelectPop(requireActivity());
        initData();
    }

    private void initData() {
        ContractKeyValueBean contractStateBean1 = new ContractKeyValueBean();
        contractStateBean1.key = "1";
        contractStateBean1.value = "拟定中";
        contractStateList.add(contractStateBean1);

        ContractKeyValueBean contractStateBean2 = new ContractKeyValueBean();
        contractStateBean2.key = "7";
        contractStateBean2.value = "待我方签署";
        contractStateList.add(contractStateBean2);
        mSelectPop2.setData(contractStateList);

        ContractKeyValueBean contractApprovalBean1 = new ContractKeyValueBean();
        contractApprovalBean1.key = "1";
        contractApprovalBean1.value = "审批中";
        contractApprovalList.add(contractApprovalBean1);

        ContractKeyValueBean contractApprovalBean2 = new ContractKeyValueBean();
        contractApprovalBean2.key = "2";
        contractApprovalBean2.value = "已通过";
        contractApprovalList.add(contractApprovalBean2);

        ContractKeyValueBean contractApprovalBean3 = new ContractKeyValueBean();
        contractApprovalBean3.key = "3";
        contractApprovalBean3.value = "已拒绝";
        contractApprovalList.add(contractApprovalBean3);
        mSelectPop1.setData(contractApprovalList);
    }

    private void loadData() {
        mViewModel.category = contractCategoryBean.getKey();
        mViewModel.getContractList(false);
    }

    @Override
    protected void initObserve() {
        // 第一页无数据显示无数据UI
        mViewModel.hasNoContractListData.observe(this, b -> mViewModel.showNoData(mDataBinding.contractRlvView));
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ContractItemModel.class, BR.contractItemModel);
    }

    @Override
    protected void initListener() {
        mDataBinding.contractRlvView.setLoadingListener(new XRecyclerView.LoadingListener() {

            @Override
            public void onRefresh() {
                mViewModel.getContractList(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getContractList(true);
            }
        });

        // adapter 操作合同按钮点击回调，如：发送合同，修改合同，查看合同等操作按钮
        mViewModel.adapter.setOnclickListener((bean) -> mViewModel.jumpToSignContractActivity(bean));
        // pop弹窗相关
        mSelectPop1.setOnDismissListener(() -> {
            mDataBinding.imgApproval.setRotation(0f);
            mViewModel.selectTab.set(0);
            mViewModel.selectTab5.set(0 != mSelectPop1.currentPosition);
            mDataBinding.tvTabContractApproval.setTextColor(0 == mSelectPop1.currentPosition ?
                    getResources().getColor(R.color.color_ff666666) : getResources().getColor(R.color.color_ffef4033));
        });
        mSelectPop2.setOnDismissListener(() -> {
            mDataBinding.imgStatus.setRotation(0f);
            mViewModel.selectTab.set(0);
            mViewModel.selectTab6.set(0 != mSelectPop2.currentPosition);
            mDataBinding.tvTabContractStatus.setTextColor(0 == mSelectPop2.currentPosition ?
                    getResources().getColor(R.color.color_ff666666) : getResources().getColor(R.color.color_ffef4033));
        });
        mDataBinding.flContractState.setOnClickListener(v -> {
            mDataBinding.tvTabContractStatus.setTextColor(getResources().getColor(R.color.color_ff333333));
            mViewModel.selectTab.set(2);
            if (baseActivity instanceof ContractActivity) {
                baseActivity.hideSoftInputFromWindow(mDataBinding.etSearch);
            }
            mSelectPop2.showPopwindow(mDataBinding.contractPopLine, 0, DisplayUtil.dip2px(requireActivity(), 1));
        });

        mDataBinding.flContractApproval.setOnClickListener(v -> {
            mDataBinding.tvTabContractApproval.setTextColor(getResources().getColor(R.color.color_ff333333));
            mViewModel.selectTab.set(1);
            if (baseActivity instanceof ContractActivity) {
                baseActivity.hideSoftInputFromWindow(mDataBinding.etSearch);
            }
            mSelectPop1.showPopwindow(mDataBinding.contractPopLine, 0, DisplayUtil.dip2px(requireActivity(), 1));
        });
        mSelectPop1.setOnItemOnClickListener(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean bean, int position) {
                mViewModel.selectTab5.set(true);
                mDataBinding.imgApproval.setRotation(180f);
                mDataBinding.tvTabContractApproval.setText(0 == position ? "审批状态" : bean.value);
                mViewModel.getContractListForApproval(bean.key);
                mSelectPop1.dismiss();
            }
        });
        mSelectPop2.setOnItemOnClickListener(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean bean, int position) {
                mViewModel.selectTab6.set(true);
                mDataBinding.imgStatus.setRotation(180f);
                mDataBinding.tvTabContractStatus.setText(0 == position ? "合同状态" : bean.value);
                mViewModel.getContractListForStatus(bean.key);
                mSelectPop2.dismiss();
            }
        });

        // 根据合同名称搜索合同
        mDataBinding.etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mViewModel.getContractListForTitle(mDataBinding.etSearch.getText().toString().trim());
            }
            return false;
        });
    }

}